import fetch from 'utils/fetch'

// 商城首页
export function getShop(data) {
  return fetch({
    url: '/shop/',
    method: 'get',
    params: data
  })
}

// 获取书城城列表
export function getShopList(data) {
  return fetch({
    url: '/shop/list/',
    method: 'get',
    params: data
  })
}

// 获取好物商城列表
export function getGoodList(data) {
  return fetch({
    url: '/shop/things/',
    method: 'get',
    params: data
  })
}

// 获取详情
export function getShopDetail(data) {
  return fetch({
    url: '/shop/detail/',
    method: 'get',
    params: data
  })
}

// 获取好物详情
export function getGoodDetail(data) {
  return fetch({
    url: '/shop/things/detail/',
    method: 'get',
    params: data
  })
}

export function getComment(data) {
  return fetch({
    url: '/shop/comment/',
    method: 'get',
    params: data
  })
}

// 查看购物车/shop/cart/
export function getShopCart(data) {
  return fetch({
    url: '/shop/cart/',
    method: 'get',
    params: data
  })
}

// 发表评论
export function pushComment(data) {
  return fetch({
    url: '/shop/submit/comment/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      const form = new FormData()
      for (const key in data) {
        form.append(key, data[key])
      }
      return form
    }],
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 加入购物车
export function addShopCart(data) {
  return fetch({
    url: '/shop/cart/add/',
    method: 'get',
    params: data
  })
}

// 减少购物车商品数量
export function reduceShopCart(data) {
  return fetch({
    url: '/shop/cart/reduce/',
    method: 'get',
    params: data
  })
}

// 删除购物车商品
export function delShopCart(data) {
  return fetch({
    url: '/shop/cart/del/',
    method: 'get',
    params: data
  })
}

// 获取订单（误）
export function getOrder(data) {
  return fetch({
    url: '/shop/order',
    method: 'get',
    params: data
  })
}

// 立即购买--提交订单
export function submitNow(data) {
  return fetch({
    url: '/shop/buy/directly/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 提交订单
export function submitOrder(data) {
  return fetch({
    url: '/shop/submit/order/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 获取收货地址
export function getAddr(data) {
  return fetch({
    url: '/shop/list/addr/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 新增收货地址
export function addAddr(data) {
  return fetch({
    url: '/shop/add/addr/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 编辑收货地址
export function editAddr(data) {
  return fetch({
    url: '/shop/edit/addr/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 删除收货地址
export function delAddr(data) {
  return fetch({
    url: '/shop/del/addr/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 订单
// 删除订单
export function deleteOrder(data) {
  return fetch({
    url: '/user/order/del/',
    method: 'get',
    params: data
  })
}

// 取消订单
export function cancelOrder(data) {
  return fetch({
    url: '/user/order/cacel/',
    method: 'get',
    params: data
  })
}

// 查看物流
export function viewExp(data) {
  return fetch({
    url: '/user/order/express/',
    method: 'get',
    params: data
  })
}